package com.meihuayishu.backend.controller;

import com.meihuayishu.backend.common.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/app")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "应用支持服务", description = "应用配置、系统公告")
public class AppSupportController {
    
    @GetMapping("/config")
    @Operation(summary = "获取应用配置", description = "获取应用全局配置信息")
    @Parameter(name = "Authorization", description = "Bearer Token", required = true)
    public ApiResponse<Map<String, Object>> getAppConfig(
            @RequestParam String version,
            @RequestParam String platform,
            @RequestHeader("Authorization") String authorization) {
        try {
            Map<String, Object> response = Map.of(
                    "appInfo", Map.of(
                            "latestVersion", "1.0.1",
                            "minSupportedVersion", "1.0.0",
                            "updateRequired", false
                    ),
                    "features", Map.of(
                            "aiInterpretationEnabled", true,
                            "voiceDivinationEnabled", true,
                            "imageDivinationEnabled", true
                    ),
                    "limits", Map.of(
                            "freeDailyDivinations", 5,
                            "maxImageSize", 10485760,
                            "maxQuestionLength", 200
                    )
            );
            return ApiResponse.success(response, "获取成功");
        } catch (Exception e) {
            log.error("获取应用配置失败", e);
            return ApiResponse.error(500, "获取配置失败");
        }
    }
    
    @GetMapping("/announcements")
    @Operation(summary = "系统公告", description = "获取系统公告信息")
    @Parameter(name = "Authorization", description = "Bearer Token", required = true)
    public ApiResponse<Map<String, Object>> getAnnouncements(
            @RequestParam(defaultValue = "ALL") String type,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestHeader("Authorization") String authorization) {
        try {
            Map<String, Object> response = Map.of(
                    "pagination", Map.of("page", page, "size", size, "total", 3, "totalPages", 1),
                    "announcements", List.of(
                            Map.of(
                                    "id", "ann_1704099000123",
                                    "title", "新年特惠活动上线！",
                                    "content", "VIP会员享受5折优惠",
                                    "type", "PROMOTION",
                                    "isRead", false,
                                    "publishedAt", LocalDateTime.now()
                            )
                    ),
                    "unreadCount", 1
            );
            return ApiResponse.success(response, "获取成功");
        } catch (Exception e) {
            log.error("获取公告失败", e);
            return ApiResponse.error(500, "获取公告失败");
        }
    }
}